package gwt.trading.client;

import gwt.trading.client.geometry.Point;

/**
 * @author Iulian Costan (iulian.costan@gmail.com)
 *
 */
public class VerticalAxis extends Axis {

	private int width;
	private float min;
	private float max;
	private int tagLenght = 6;

	public VerticalAxis(float min, float max) {
		this(min, max, TradingChartSettings.VERTICAL_AXIS_WIDTH);
	}

	public VerticalAxis(float min, float max, int width) {
		this.min = (float) Math.floor(min);
		this.max = (float) Math.ceil(max);
		this.width = width;
	}

	public void draw(Renderer renderer) {
		drawAxisLine(renderer);
		drawLabels(renderer);
	}

	private void drawLabels(Renderer renderer) {
		int yMaxLabelPosition = getDrawingManager().getYPosition(getMax());
		int yMidLabelPosition = getDrawingManager().getYPosition(getMin() + (getMax() - getMin()) / 2);
		int yMinLabelPosition = getDrawingManager().getYPosition(getMin());
		int xLabelPosition = getDrawingBoard().getMinXPosition();

		// draw min line
		Point start = new Point(xLabelPosition, yMinLabelPosition);
		Point end = new Point(xLabelPosition + tagLenght, yMinLabelPosition);
		renderer.drawLine(new Line(start, end, getColor()));

		// draw mid line
		start = new Point(xLabelPosition, yMidLabelPosition);
		end = new Point(xLabelPosition + tagLenght, yMidLabelPosition);
		renderer.drawLine(new Line(start, end, getColor()));
		
		// draw max line
		start = new Point(xLabelPosition, yMaxLabelPosition);
		end = new Point(xLabelPosition + tagLenght, yMaxLabelPosition);
		renderer.drawLine(new Line(start, end, getColor()));
	}

	private void drawAxisLine(Renderer renderer) {
		int xPosition = getDrawingBoard().getMinXPosition();
		int yMinPosition = getDrawingBoard().getMinYPosition();
		int yMaxPosition = getDrawingBoard().getMaxYPosition();

		Point start = new Point(xPosition, yMinPosition);
		Point end = new Point(xPosition, yMaxPosition);

		// draw vertical line
		renderer.drawLine(new Line(start, end, getColor()));
	}

	// TODO width is common for all areas, move it
	public int getWidth() {
		return width;
	}

	public float getMax() {
		return max;
	}

	public float getMin() {
		return min;
	}

}
